package cn.edu.neusoft.food229.model;

import cn.edu.neusoft.food229.bean.Result;
import cn.edu.neusoft.food229.iface.Registeriface;
import cn.edu.neusoft.food229.listener.ResultListener;
import cn.edu.neusoft.food229.service.UserService;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import static cn.edu.neusoft.food229.Commen.Commen.BASEURL;

public class RegisterModel implements Registeriface {
    private Retrofit retrofit;
    public RegisterModel(){
        retrofit=new Retrofit.Builder()
                .baseUrl(BASEURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    @Override
    public void getRegisterResult(String username, String userpass, String mobilenum, String address,
                                  String comment, final ResultListener resultListener) {
        UserService userService=retrofit.create(UserService.class);
        Call<Result> call
                = userService.register(username, userpass,mobilenum,address,comment);
        call.enqueue(new Callback<Result>() {

            public void onResponse(Call<Result> call, Response<Result> response) {
                if(response.isSuccessful() &&  response!=null)
                {
                    resultListener.onResponce(response.body());
                }
                else resultListener.onFail("onresponse fail");


            }
            public void onFailure(Call<Result> call, Throwable t) {
                System.out.println("--网络错误");
                resultListener.onFail(t.toString());
            }
        });

    }
}
